<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot-scope="scope" slot="menu">
        <el-button
          v-if="permission.store_view"
          icon="el-icon-view"
          size="small"
          type="text"
          @click.stop="handleDetail(scope.row)"
          >详 情</el-button
        >
        <el-button
          v-if="permission.store_view"
          icon="el-icon-cpu"
          size="small"
          type="text"
          @click.stop="handleDevice(scope.row)"
          >台卡管理</el-button
        >
      </template>
      <template slot="menuLeft">
        <!-- <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.store_delete"
          @click="handleDelete"
          >删 除
        </el-button> -->
      </template>
      <template slot="menuRight">
        <!-- <el-button
          type="success"
          size="small"
          plain
          icon="el-icon-upload2"
          v-if="permission.store_import"
          @click="handleImport"
          >导入
        </el-button> -->
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permission.store_export"
          @click="handleExport"
          >导出
        </el-button>
      </template>
      <template slot-scope="{ row }" slot="isOn">
        <el-switch
          :active-value="1"
          :inactive-value="0"
          v-model="row.isOn"
          @change="handleSwitch(row)"
        >
        </el-switch>
      </template>
      <template slot-scope="{}" slot="serviceNoSearch">
        <select-page ref="searchSerSelect" label="serviceName" value="serviceNo" :select.sync="search.serviceNo" :url="website.searchLink.serviceUrl" placeholder-text="服务商" filter-key="searchParam"></select-page>
      </template>
      <template slot-scope="{}" slot="orgNoSearch">
        <select-page ref="searchOrgSelect" label="serviceName" value="serviceNo" :select.sync="search.orgNo" :url="website.searchLink.orgUrl" placeholder-text="所属机构" filter-key="searchParam"></select-page>
      </template>
      <template slot-scope="{}" slot="rpiNoSearch">
        <select-page ref="searchRpiSelect" label="rpiName" value="rpiNo" :select.sync="search.rpiNo" :url="website.searchLink.merchantUrl" placeholder-text="商户" filter-key="searchParam"></select-page>
      </template>
    </avue-crud>
    <!-- <el-dialog
      title="数据导入"
      append-to-body
      :visible.sync="excelBox"
      width="555px"
    >
      <avue-form
        :option="excelOption"
        v-model="excelForm"
        :upload-after="uploadAfter"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog> -->
  </basic-container>
</template>

<script>
import {
  getList,
  // getDetail,
  add,
  // update,
  remove,
  getExport,
  getExportTemplate,
  incomingTerminalOn
} from "@/api/rb-oem/terminal/terminal";
import {
  selectModelList
} from "@/api/rb-oem/store/store";
import { mapGetters } from "vuex";
import { downloadFileExcel } from "@/util/util";
import selectPage from "@/components/select-page/select-page";

var serviceDic = [{
  label:'包含下级',
  value: 1
}]
export default {
  props: {
    rptNo: {
      type: String,
      required: '',
    }
  },
  components: {
    selectPage,
  },
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: true,
      excelBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        searchIndex: 3, //从第三个后隐藏 searchIcon
        searchIcon: true, //显示搜索 展开/隐藏按钮
        selection: false,
        searchMenuSpan: 6,
        columnBtn: false,
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            sortable: true,
            display: false,
            hide: true,
          },
          {
            label: "服务商编号",
            searchLabel: "服务商",
            prop: "serviceNo",
            type: 'select',
            search: true,
            minWidth: 110,
            overHidden:true,
            rules: [
              {
                required: true,
                message: "请输入服务商编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "服务商名称",
            prop: "serviceName",
            type: "input",
            display: false,
            rules: [
              {
                required: true,
                message: "请输入服务商名称",
                trigger: "blur",
              },
            ],
            minWidth: 250,
            overHidden: true,
          },
          {
            label: '',
            prop: 'isAllSer',
            type: 'select',
            multiple: false,
            dataType: "string",
            showColumn: false,
            hide: true,
            search: true,
            searchType:"checkbox",
            searchSpan: 0,
            searchLabelWidth: '0',
            dicData: serviceDic
          },
          {
            label: "所属机构",
            prop: "orgNo",
            type: "select",
            dataType: "string",
            showColumn: false,
            search: true,
            filterable: true,
            hide: true,
            rules: [
              {
                required: true,
                message: "请选择所属机构",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "产品类型",
          //   prop: "productTypeName",
          //   type: "select",
          //   search: true,
          //   dicUrl: "/api/blade-system/dict/dictionary?code=product_type",
          //   props: {
          //     label: "dictValue",
          //     value: "dictKey",
          //   },
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入产品类型",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          // {
          //   label: "台卡类型",
          //   prop: "rptNo",
          //   type: "select",
          //   dicUrl: "/api/rb-oem/oem/store/store/selectTypeList?current=-1&size=-1",
          //   props: {
          //     label: "rptName",
          //     value: "rptNo",
          //     desc: "rptNo"
          //   },
          //   rules: [
          //     {
          //       required: true,
          //       message: "请选择设备类型",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          // {
          //   label: "台卡类型",
          //   prop: "rptName",
          //   type: "input",
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入台卡类型",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: "台卡品牌",
            prop: "rpsNo",
            type: "select",
            search: true,
            hide: true,
            dicUrl: "/api/rb-oem/oem/store/store/selectSupList?current=-1&size=-1&rptNo=15",
            props: {
              label: "rpsName",
              value: "rpsNo",
              desc: "rpsNo"
            },
            change: (row) => {
              if (row.value) {
                // 获取供应商
                this.form.rpmNo = '';
                this.search.rpmNo = '';
                selectModelList(-1,-1,{rpsNo:row.value,rptNo: '15'}).then((res)=>{
                  const column = this.findObject(this.option.column, "rpmNo");
                  column.dicData = res.data.data;
                })
              }
            },
            rules: [
              {
                required: true,
                message: "请选择台卡品牌",
                trigger: "blur",
              },
            ],
          },
          {
            label: "台卡品牌",
            prop: "rpsName",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入台卡品牌",
                trigger: "blur",
              },
            ],
            minWidth: 100,
            overHidden: true,
          },
          {
            label: "台卡型号",
            prop: "rpmNo",
            type: "select",
            search: true,
            hide: true,
            dicUrl: "/api/rb-oem/oem/store/store/selectModelList?current=-1&size=-1&rptNo=15",
            props: {
              label: "rpmName",
              value: "rpmNo",
              desc: "rpmNo"
            },
            rules: [
              {
                required: true,
                message: "请选择台卡型号",
                trigger: "blur",
              },
            ],
            minWidth: 100,
            overHidden: true,
          },
          {
            label: "台卡型号",
            prop: "rpmName",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入台卡型号",
                trigger: "blur",
              },
            ],
            minWidth: 100,
            overHidden: true,
          },
          // {
          //   label: "设备归属",
          //   prop: "belongTo",
          //   type: "select",
          //   search: true,
          //   dicUrl: "/api/blade-system/dict/dictionary?code=belong_to",
          //   props: {
          //     label: "dictValue",
          //     value: "dictKey",
          //   },
          //   rules: [
          //     {
          //       required: true,
          //       message: "请选择设备归属",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: "台卡编号",
            prop: "rsSn",
            type: "input",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入台卡编号",
                trigger: "blur",
              },
            ],
            overHidden:true
          },
          {
            label: "台卡状态",
            prop: "isOn",
            type: "select",
            width: 70,
            dicUrl: "/api/blade-system/dict/dictionary?code=is_on",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            display: false,
            rules: [
              {
                required: true,
                message: "请选择台卡状态",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "商户编号",
            searchLabel: "商户",
            prop: "rpiNo",
            type: 'select',
            width: 140,
            search: true,
            overHidden:true
          },
          {
            label: "门头名称",
            prop: "rpiName",
            type: "input",
            minWidth: 200,
            overHidden: true,
          },
          {
            label: "绑定日期",
            prop: "createTime",
            type: "input",
            minWidth: 140,
            overHidden: true,
          },
        ],
      },
      data: [],
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "模板上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "模板上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data",
            },
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: "/api/rb-oem/terminal/terminal/import",
          },
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.store_add, false),
        viewBtn: this.vaildData(this.permission.store_view, false),
        delBtn: this.vaildData(this.permission.store_delete, false),
        editBtn: this.vaildData(this.permission.store_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    /**
     * @func 详情
     * @desc 点击详情进入详情界面
     * @param {Object} row - 商户信息
     * */
    handleDetail(row) {
      this.$router.push({
        name: "商户详情",
        query: {
          d: this.$crypto.encrypt(JSON.stringify({
            rpiNo: row.rpiNo,
            rpiExStatus: row.rpiExStatus
          }))
        },
      });
    },
    /**
     * @func 终端状态开关
     * @desc 终端状态开关 0否 1是
     * @param {Object} row - 终端信息
     * */
    handleSwitch(row) {
      incomingTerminalOn(row.id, row.isOn).then(
        () => {},
        (error) => {
          row.isOn = row.isOn == 1 ? 0 : 1;
          window.console.log(error);
        }
      );
    },
    /**
     * @func 设备管理
     * @desc 点击设备管理进入设备管理界面
     * @param {Object} row - 仓库信息
     * */
    handleDevice(row) {
      this.$router.push({
        name: "终端管理",
        query: {
          rpiNo: row.rpiNo
        },
      });
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    /* rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    }, */
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        // getDetail(this.form.id).then((res) => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, {rptNo: '15'})
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleImport() {
      this.excelBox = true;
    },
    handleExport() {
      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        getExport(this.search).then((res) => {
            downloadFileExcel(res);
          },
          (error) => {
            window.console.log(error);
          }
        );
      });
    },
    handleTemplate() {
      getExportTemplate().then((res) => {
          downloadFileExcel(res);
        },
        (error) => {
          window.console.log(error);
        }
      );
    },
    uploadAfter(res, done) {
      this.excelBox = false;
      this.refreshChange();
      done();
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
  },
};
</script>

<style>
</style>
